package princessWorld;

import processing.core.*;

public class SimplePrincessWorld implements IPrincessWorld{
	
	private PApplet p;
	
	//World floor
	private float floorTileSize = 20;
	private int floorHorisontalTiles = (int) (SIZE.x / floorTileSize);
	private int floorVerticalTiles = (int) (SIZE.y / floorTileSize);
	private PVector[][] floor = new PVector[floorHorisontalTiles][floorVerticalTiles];
	private float floorMaxDepth = SIZE.z;
	private float floorMinDepth = -50;
	private float floorNoiseScale = 0.2f;
	
	//Critters
	private Critters critters;	
	
	public void setup(PApplet processing) {
		p = processing;
		
		critters = new Critters();
		critters.setup(p);
		
		setupFloor();
	}
	
	private void setupFloor() {
		for(int x = 0; x < floorHorisontalTiles; x ++) {
			for(int y = 0; y < floorVerticalTiles; y ++) {
				float xPos = x * (floorTileSize + 1);
				float yPos = y * (floorTileSize + 1);
				
				float noiseFactor = p.noise(x * floorNoiseScale, y * floorNoiseScale);
				float zPos = floorMinDepth + (floorMaxDepth - floorMinDepth) * noiseFactor;
				
				floor[x][y] = new PVector(xPos, yPos, zPos);
			}
		}
	}
	
	public void update() {
		critters.update();
	}
	
	public void draw() {
		//World boundaries	
		drawWorldBoundaries();
		
		//Floor	
		drawFloor();
		
		//Critter flock
		critters.draw();
	}
	
	private void drawWorldBoundaries() {
		p.fill(32, 200);
		p.stroke(128);
		p.strokeWeight(1.5f);
		//Left wall
		p.beginShape(PApplet.QUADS);
			p.vertex(floor[0][0].x, floor[0][0].y, 0);	
			p.vertex(floor[0][0].x, floor[0][0].y, SIZE.z);	
			p.vertex(floor[0][floorVerticalTiles -1].x, floor[0][floorVerticalTiles -1].y, SIZE.z);	
			p.vertex(floor[0][floorVerticalTiles -1].x, floor[0][floorVerticalTiles -1].y, 0);
		p.endShape();
		//RIght wall
		p.beginShape(PApplet.QUADS);
			p.vertex(floor[floorHorisontalTiles -1][0].x, floor[floorHorisontalTiles -1][0].y, 0);	
			p.vertex(floor[floorHorisontalTiles -1][0].x, floor[floorHorisontalTiles -1][0].y, SIZE.z);	
			p.vertex(floor[floorHorisontalTiles -1][floorVerticalTiles -1].x, floor[floorHorisontalTiles -1][floorVerticalTiles -1].y, SIZE.z);	
			p.vertex(floor[floorHorisontalTiles -1][floorVerticalTiles -1].x, floor[floorHorisontalTiles -1][floorVerticalTiles -1].y, 0);
		p.endShape();
		//Upper wall
		p.beginShape(PApplet.QUADS);
			p.vertex(floor[0][0].x, floor[0][0].y, 0);	
			p.vertex(floor[0][0].x, floor[0][0].y, SIZE.z);	
			p.vertex(floor[floorHorisontalTiles -1][0].x, floor[floorHorisontalTiles -1][0].y, SIZE.z);	
			p.vertex(floor[floorHorisontalTiles -1][0].x, floor[floorHorisontalTiles -1][0].y, 0);	
		p.endShape();	
		//Lower wall
		p.beginShape(PApplet.QUADS);
			p.vertex(floor[0][floorVerticalTiles -1].x, floor[0][floorVerticalTiles -1].y, 0);	
			p.vertex(floor[0][floorVerticalTiles -1].x, floor[0][floorVerticalTiles -1].y, SIZE.z);
			p.vertex(floor[floorHorisontalTiles -1][floorVerticalTiles -1].x, floor[floorHorisontalTiles -1][floorVerticalTiles -1].y, SIZE.z);	
			p.vertex(floor[floorHorisontalTiles -1][floorVerticalTiles -1].x, floor[floorHorisontalTiles -1][floorVerticalTiles -1].y, 0);
		p.endShape();
	}
	
	private void drawFloor() {
		p.fill(64, 200);
		p.stroke(128);
		p.strokeWeight(1f);
		for(int x = 1; x < floorHorisontalTiles; x ++) {
			for(int y = 1; y < floorVerticalTiles; y ++) {	
				p.beginShape(PApplet.QUADS);				
					p.vertex(floor[x][y].x, floor[x][y].y, floor[x][y].z);	
					p.vertex(floor[x-1][y].x, floor[x-1][y].y, floor[x-1][y].z);	
					p.vertex(floor[x-1][y-1].x, floor[x-1][y-1].y, floor[x-1][y-1].z);	
					p.vertex(floor[x][y-1].x, floor[x][y-1].y, floor[x][y-1].z);
				p.endShape();
			}
		}		
	}

	public void addButterfly(PVector position) {
		System.out.println("Butterfly added at " + position);
	}
	
}
